import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import enTranslation from "./locales/en.json";
import zhTranslation from "./locales/zh.json";

const resources = {
  en: {
    ...enTranslation,
  },
  zh: {
    ...zhTranslation,
  },
};

i18n
  // 检测用户语言
  .use(LanguageDetector)
  // 将 i18n 实例传递给 react-i18next
  .use(initReactI18next)
  // 初始化 i18next
  .init({
    resources,
    fallbackLng: "zh", // 使用英语作为后备语言
    debug: process.env.NODE_ENV === "development",
    interpolation: {
      escapeValue: false, // 不转义 HTML
    },
    react: {
      transSupportBasicHtmlNodes: true, // 允许基本 HTML 标签
      transKeepBasicHtmlNodesFor: ["br", "strong", "i", "span", "a"], // 允许保留的标签
    },
    // 检测语言时的配置
    detection: {
      order: ["localStorage", "navigator"],
      caches: ["localStorage"],
    },
  });

export default i18n;
